﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Kevin.Shop.Business.Core.Components.Responser;
using Kevin.Shop.Business.SalesManagement.Components;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Modle.MyAccount;
using RiderShop.Mobile.ServiceEntity.MyAccount;
using RiderShop.Mobile.Common.Component;

namespace RiderShop.WebSite.Controllers.MyAccount
{
    public partial class MyAccountController : ThemeController
    {
        #region 得到用户的收货地址列表
        /// <summary>
        /// 用户收货地址列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public ActionResult ShippingAddress()
        {
            List<UserShipingAddressExtend> shippingAddressList = MyAccountModel.GetShippingAddressList(WebClientContext.Current.UserId);

            return View(WebClientContext.Current.SiteTheme, "MyAccount", "ShippingAddress", WebClientContext.Current.CacheKeyDefaultMasterPage, shippingAddressList);
        }
        #endregion

        #region 创建用户的收货地址
        /// <summary>
        /// 删除用户的某个收货地址
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public ActionResult ShippingAddressCreate()
        {
            UserShipingAddressExtend shipingAddress = new UserShipingAddressExtend();
            List<RegionBasicInfo> regionList = new List<RegionBasicInfo>();
            regionList = MyAccountModel.GetRegionListByParentId(0);
            shipingAddress.RegionList = regionList;

            return View(WebClientContext.Current.SiteTheme, "MyAccount", "ShippingAddressCreate", WebClientContext.Current.CacheKeyDefaultMasterPage, shipingAddress);
        }

        /// <summary>
        /// 创建用户的收货地址
        /// </summary>
        /// <param name="userShipingAddressExtend">收货地址对象</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public ActionResult ShippingAddressCreate(UserShipingAddressExtend userShipingAddressExtend)
        {
            userShipingAddressExtend.UserId = WebClientContext.Current.UserId;
            userShipingAddressExtend.EmailAddress = string.IsNullOrEmpty(userShipingAddressExtend.EmailAddress) ? string.Empty : userShipingAddressExtend.EmailAddress;
            userShipingAddressExtend.IsDefault = userShipingAddressExtend.IsDefault == null ? false : userShipingAddressExtend.IsDefault;
            userShipingAddressExtend.TelPhone = string.IsNullOrEmpty(userShipingAddressExtend.TelPhone) ? string.Empty : userShipingAddressExtend.TelPhone;

            var action = Request.QueryString["action"];

            if (!userShipingAddressExtend.IsDefault.HasValue && !string.IsNullOrEmpty(action))
            {
                userShipingAddressExtend.IsDefault = true;
            }

            ServiceResponser result = MyAccountModel.AddShippingAddress(userShipingAddressExtend);
            if (result.Status && !string.IsNullOrEmpty(action))
            {
                if (action.ToLower() == "order")
                {
                    return Redirect("~/Purchase/CheckOut");
                }
                else if (action == "cart")
                {
                    return Redirect("~/Purchase/ShoppingCart");
                }
            }

            return Redirect("~/MyAccount/ShippingAddress");
        }
        #endregion

        #region 编辑用户的某个收货地址
        /// <summary>
        /// 编辑用户的某个收货地址
        /// </summary>
        /// <param name="userId">当前用户编号</param>
        /// <param name="shippingId">当前收货地址编号</param>
        /// <param name="number">当前第几个收货地址</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public ActionResult ShippingAddressEdit(int userId, int shippingId, int number)
        {
            List<UserShipingAddressExtend> shippingAddress = MyAccountModel.GetShippingAddress(userId, shippingId);
            List<RegionBasicInfo> regionList = new List<RegionBasicInfo>();
            regionList = MyAccountModel.GetRegionListByParentId(0);
            shippingAddress[0].RegionList = regionList;

            if (shippingAddress.Count > 0)
            {
                return View(WebClientContext.Current.SiteTheme, "MyAccount", "ShippingAddressEdit", "_MobileLayout", shippingAddress);
            }

            return View(WebClientContext.Current.SiteTheme, "MyAccount", "ShippingAddressEdit", "_MobileLayout", null);
        }

        /// <summary>
        /// 编辑用户的某个收货地址
        /// </summary>
        /// <param name="collection">表单集合对象</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public ActionResult ShippingAddressEdit(UserShipingAddressExtend userShipingAddressExtend)
        {
            userShipingAddressExtend.UserId = WebClientContext.Current.UserId;
            userShipingAddressExtend.EmailAddress = string.IsNullOrEmpty(userShipingAddressExtend.EmailAddress) ? string.Empty : userShipingAddressExtend.EmailAddress;
            userShipingAddressExtend.IsDefault = userShipingAddressExtend.IsDefault == null ? false : userShipingAddressExtend.IsDefault;
            userShipingAddressExtend.TelPhone = string.IsNullOrEmpty(userShipingAddressExtend.TelPhone) ? string.Empty : userShipingAddressExtend.TelPhone;

            ServiceResponser result = MyAccountModel.EditShippingAddress(userShipingAddressExtend);
            if (result.Status)
            {
                var action = Request.QueryString["action"];
                if (action == "cart")
                {
                    return Redirect("~/Purchase/ShoppingCart");
                }
                else if (action == "order")
                {
                    return Redirect("~/Purchase/CheckOut");
                }
                return Redirect("~/MyAccount/ShippingAddress");
            }

            return Redirect("~/MyAccount/ShippingAddress");
        }
        #endregion

        #region 查看收货地址信息
        /// <summary>
        /// 查看某个收货地址
        /// </summary>
        /// <param name="shippingId">收货地址编号</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public JsonResult ShippingAddressView(int shippingId)
        {
            List<UserShipingAddressExtend> shippingAddress = MyAccountModel.GetShippingAddress(WebClientContext.Current.UserId, shippingId);

            return Json(shippingAddress.FirstOrDefault(), JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 删除用户的某个收货地址
        /// <summary>
        /// 删除用户的某个收货地址
        /// </summary>
        /// <param name="collection">表单集合对象</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public ActionResult ShippingAddressDelete(FormCollection collection)
        {
            UserShipingAddressExtend shipingAddress = new UserShipingAddressExtend();
            shipingAddress.UserId = Convert.ToInt32(collection["hidUserId"]);
            shipingAddress.ShippingId = Convert.ToInt32(collection["hidShippingId"]);

            ServiceResponser result = MyAccountModel.DeleteShippingAddress(shipingAddress.ShippingId);

            return Redirect("ShippingAddress?userId=" + shipingAddress.UserId + "&action=" + Request.QueryString["action"]);
        }
        #endregion

        #region 设置默认的收货地址
        /// <summary>
        /// 设置默认的收货地址
        /// </summary>
        /// <param name="shippingId"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public ActionResult SetDefaultShippingAddress(int shippingId)
        {
            int userId = WebClientContext.Current.UserId;
            List<UserShipingAddressExtend> shippingAddressList = MyAccountModel.GetShippingAddress(userId, shippingId);
            if (shippingAddressList.Count > 0)
            {
                shippingAddressList[0].IsDefault = true;
                ServiceResponser result = MyAccountModel.EditShippingAddress(shippingAddressList[0]);

            }

            var action = Request.QueryString["action"];
            if (!string.IsNullOrEmpty(action))
            {
                if (action.ToLower() == "order")
                {
                    return Redirect("~/Purchase/CheckOut");
                }
                else if (action == "cart")
                {
                    return Redirect("~/Purchase/ShoppingCart");
                }
            }

            return Redirect("~/MyAccount/ShippingAddress?" + "action=" + Request.QueryString["action"]);
        }
        #endregion

        #region 加载省,市,区
        /// <summary>
        /// 得到区域
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult RegionLoad()
        {
            List<RegionBasicInfo> regionList = new List<RegionBasicInfo>();
            regionList = MyAccountModel.GetRegionListByParentId(0);

            return Json(regionList, JsonRequestBehavior.AllowGet);
        }
        #endregion

        [HttpGet]
        [Authorize]
        public ActionResult ShippingAddressSelect()
        {
            int userId = WebClientContext.Current.UserId;
            List<UserShipingAddressExtend> shippingAddressList = MyAccountModel.GetShippingAddressList(userId);
            @ViewBag.Number = 1;
            if (shippingAddressList.Count > 0)
            {
                return View(shippingAddressList);
            }

            return View();
        }

        #region CheckOut界面的收货地址信息
        /// <summary>
        /// 得到用户收货地址的列表
        /// </summary>
        /// <param name="shippingId"></param>
        /// <returns></returns>
        public ActionResult ShippingAddressPartial(int shippingId = 0)
        {
            UserShipingAddressExtend model = MyAccountModel.GetShippingAddressWithRegionList(shippingId);

            return PartialView(model);
        }

        /// <summary>
        /// 新增或者编辑收货地址
        /// </summary>
        /// <param name="userShipingAddressExtend"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public ActionResult ShippingAddressPartial(UserShipingAddressExtend userShipingAddressExtend)
        {
            userShipingAddressExtend.UserId = WebClientContext.Current.UserId;
            userShipingAddressExtend.EmailAddress = string.IsNullOrEmpty(userShipingAddressExtend.EmailAddress) ? string.Empty : userShipingAddressExtend.EmailAddress;
            userShipingAddressExtend.IsDefault = userShipingAddressExtend.IsDefault == null ? false : userShipingAddressExtend.IsDefault;
            userShipingAddressExtend.TelPhone = string.IsNullOrEmpty(userShipingAddressExtend.TelPhone) ? string.Empty : userShipingAddressExtend.TelPhone;

            ServiceResponser result = new ServiceResponser();
            //新增收货地址
            if (userShipingAddressExtend.ShippingId == 0)
            {
                result = MyAccountModel.AddShippingAddress(userShipingAddressExtend);
            }
            else
            {//编辑收货地址
                result = MyAccountModel.EditShippingAddress(userShipingAddressExtend);
            }
            List<UserShipingAddressExtend> shippingAddressList = MyAccountModel.GetShippingAddressList(WebClientContext.Current.UserId);

            return Json(shippingAddressList, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
